/**
 * 
 */
package cu.ds.logicalsync.fytasrealm.communication;

import java.util.Date;

import cu.ds.logicalsync.fytasrealm.communication.constants.MapStatusRequest;
import net.jxta.document.MimeMediaType;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.endpoint.WireFormatMessage;
import net.jxta.endpoint.WireFormatMessageFactory;
import net.jxta.endpoint.Message.ElementIterator;
import net.jxta.util.CountingOutputStream;
import net.jxta.util.DevNullOutputStream;

/**
 * Utility class for JXTA message passing operation
 * 
 * @author r.vashira
 */
public final class JxtaUtil {
	public static final String REQUEST_TYPE = "RequestType";
	
	/**
	 * Create Request Message 
	 * @return
	 */
	public static Message createRequestMessage(MapStatusRequest requestType, String namespace) {
		Message msg = new Message();
		Date date = new Date(System.currentTimeMillis());
		StringMessageElement sme = new StringMessageElement(requestType.toString(), date.toString(), null);
		msg.addMessageElement(namespace, sme);
		
		addStringToMessage(msg, namespace, REQUEST_TYPE, requestType.toString());
		
		return msg;
	}
	
	public static void addStringToMessage(Message msg, String nameSpace, String elementName, String content) {
		msg.addMessageElement(nameSpace, new StringMessageElement(elementName, content, null));
	}
	
	public static String getStringFromMessage(Message msg, String nameSpace, String elementName) {
		MessageElement me = msg.getMessageElement(nameSpace, elementName);
		
		if(null != me) {
			return me.toString();
		}
		
		return null;
	}
	
	/**
     * Dumps the message content to stdout
     *
     * @param msg     the message
     * @param verbose dumps message element content if true
     */
    public static void printMessageStats(Message msg, boolean verbose) {
        try {
            CountingOutputStream cnt;
            ElementIterator it = msg.getMessageElements();

            System.out.println("------------------Begin Message---------------------");
            WireFormatMessage serialed = WireFormatMessageFactory.toWire(msg, new MimeMediaType("application/x-jxta-msg"), null);

            System.out.println("Message Size :" + serialed.getByteLength());
            while (it.hasNext()) {
                MessageElement el = it.next();
                String eName = el.getElementName();

                cnt = new CountingOutputStream(new DevNullOutputStream());
                el.sendToStream(cnt);
                long size = cnt.getBytesWritten();

                System.out.println("Element " + eName + " : " + size);
                if (verbose) {
                    System.out.println("[" + el + "]");
                }
            }
            System.out.println("-------------------End Message----------------------");
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
}
